package com.hzhjhh.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.core.bean.BeanUtil;

/**
 * 字段映射工具类
 * 	将数据库查询到的entity字段，映射到要展示的列表上
 * ShowConstants：列表展示时，表头的顺序，字段为中文
 * Enums包：数据库列名称与列表展示中的表头映射关系
 * VO包：列表展示中的表头类
 * 
 * @author 12083
 *
 */
public class FieldMapping {

	/**
	 * 从entity映射为展示列表结果
	 * 
	 * @param source 查询到的结果entity
	 * @param showClazz	列表展示的 VO类
	 * @param mapping	映射关系，一般由entity.enums包下的枚举定义，调用descMap方法得到
	 * @param headShowList	列表展示的表头
	 * @return
	 */
	public static Vector<String> entity2Show(Object source, Class showClazz, Map<String, String> mapping, List<String> headShowList) {
		JSONObject object = JSON.parseObject(JSON.toJSONString(BeanUtil.copyProperties(source, showClazz)));
		Vector<String> vectorRow = new Vector<>();
		headShowList.forEach(head -> {
			String code = mapping.get(head);
			vectorRow.add(object.getString(code));
		});
		return vectorRow;
	}
	public static Vector<String> entity2Show(Object source, Map<String, String> mapping, List<String> headShowList) {
		JSONObject object = JSON.parseObject(JSON.toJSONString(source));
		Vector<String> vectorRow = new Vector<>();
		headShowList.forEach(head -> {
			String code = mapping.get(head);
			vectorRow.add(object.getString(code));
		});
		return vectorRow;
	}
	
	/**
	 * 从entity映射为展示列表结果
	 * 
	 * @param sourceList 查询到的结果entity集合
	 * @param clazz	列表展示的 VO类
	 * @param mapping	映射关系，一般由entity.enums包下的枚举定义，调用toMap方法得到
	 * @param showList	列表展示的表头
	 * @return
	 */
	public static Vector<Vector<Object>> entity2Show(List<Object> sourceList, Class<?> clazz,Map<String, String> mapping, List<String> showList) {
		Vector<Vector<Object>> vectorResult = new Vector<>();
		List<JSONObject> jsonObjectList = sourceList.stream()
			.map(source -> JSON.parseObject(JSON.toJSONString(BeanUtil.copyProperties(source, clazz))))
			.collect(Collectors.toList());
		jsonObjectList.stream()
			.map(jsObj -> {
				Map<String, Object> showMap = new HashMap<>();
				mapping.forEach((cloumn, showName) -> showMap.put(showName, jsObj.get(cloumn)));
				return showMap;
			})
			.forEach(showMap -> {
				Vector<Object> vectorRow = new Vector<>();
				showList.forEach(colName -> vectorRow.add(showMap.get(colName)));
				vectorResult.add(vectorRow);
			});
		
		return vectorResult;
	}
	public static <T> Vector<Vector<Object>> list2Show(List<T> sourceList, Map<String, String> descMap, List<String> showList) {
		Vector<Vector<Object>> vectorResult = new Vector<>();
		List<JSONObject> jsonObjectList = sourceList.stream()
				.map(source -> JSON.parseObject(JSON.toJSONString(source)))
				.collect(Collectors.toList());
		jsonObjectList.stream()
			/*.map(sourceObj -> {
				Map<String, Object> showMap = new HashMap<>();
				mapping.forEach((cloumn, showName) -> showMap.put(showName, sourceObj.get(cloumn)));
				return showMap;
			})*/
			.forEach(sourceObj -> {
				Vector<Object> vectorRow = new Vector<>();
				showList.forEach(colName -> {
					String colCode = descMap.get(colName);
					vectorRow.add(sourceObj.get(colCode));
				});
				vectorResult.add(vectorRow);
			});
		
		return vectorResult;
	}
}
